package me.zhengjie.modules.quartz.config

import me.zhengjie.modules.quartz.domain.QuartzJob
import me.zhengjie.modules.quartz.repository.QuartzJobRepository
import me.zhengjie.modules.quartz.utils.QuartzManage
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component
import java.util.function.Consumer

/**
 * @author Kuki Wu
 * @date 2019-01-07
 */
@Component
class JobRunner(private val quartzJobRepository: QuartzJobRepository, private val quartzManage: QuartzManage) :
    ApplicationRunner {
    /**
     * 项目启动时重新激活启用的定时任务
     * @param applicationArguments /
     */
    override fun run(applicationArguments: ApplicationArguments) {
        println("--------------------注入定时任务---------------------")
        val quartzJobs = quartzJobRepository.findByIsPauseIsFalse()
        quartzJobs!!.forEach(Consumer { quartzJob: QuartzJob? -> quartzManage.addJob(quartzJob) })
        println("--------------------定时任务注入完成---------------------")
    }
}